package beans;


import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import model.Casa;
import conversions.Conversions;

/**
 * Session Bean implementation class CasaDAO
 */
@Stateless

public class CasaDAO implements CasaDAORemote {

	
	@PersistenceContext
	EntityManager em;
	
    /**
     * Default constructor. 
     */
    public CasaDAO() {
        // TODO Auto-generated constructor stub
    }
    
    public void insertCasa(CasaDTO casa) {

		Casa pr = new Conversions().fromDTO(casa);

		em.persist(pr);
	}
    
    @Override
	public List<CasaDTO> getCasaList() {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Casa> cq = cb.createQuery(Casa.class);
		Root<Casa> prop = cq.from(Casa.class);

		// cq.select(casa).where(cb.ge(casa.get(Casa_.pret),pret));

		cq.select(prop);

		TypedQuery<Casa> q = em.createQuery(cq);
		List<Casa> props = q.getResultList();

		List<CasaDTO> caseDTO = new ArrayList<>();
		for (Casa p : props) {
			caseDTO.add(new Conversions().toDTO(p));
		}
		return caseDTO;
	}

}
